package com.myhkvideo.yulongbpcdemo.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.myhkvideo.yulongbpcdemo.entity.RunningRateDayInfoData;
import com.myhkvideo.yulongbpcdemo.entity.RunningRateDayInfoData;
import com.myhkvideo.yulongbpcdemo.mapper.RunningRateDayInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RunningRateDayInfoService {

    private static final String API_URL = "http://ylty.westmining.com:60016/api/BoardProductionCommand/RunningRateDayInfo";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoginService loginService;
    @Autowired
    private ObjectMapper objectMapper;

    public ResponseEntity<String> getRunningRateDayInfo() {
        String token = loginService.getNewToken();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Authorization", "Bearer " + token);
        HttpEntity<String> requestEntity = new HttpEntity<>(headers);
        return restTemplate.exchange(API_URL, HttpMethod.POST, requestEntity, String.class);
    }

    /**
     * 获取并转换数据为 RunningRateDayInfoData 列表
     * @return RunningRateDayInfoData 列表
     * @throws IOException
     * @throws ParseException
     */
    public List<RunningRateDayInfoData> getAndConvertRunningRateDayInfo() throws IOException, ParseException {
        ResponseEntity<String> response = getRunningRateDayInfo();
        Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), Map.class);
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) responseBody.get("Data");
        return convertToRunningRateDayInfoDataList(dataList);
    }

    private List<RunningRateDayInfoData> convertToRunningRateDayInfoDataList(List<Map<String, Object>> dataList) throws ParseException {
        List<RunningRateDayInfoData> resultList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (Map<String, Object> dataMap : dataList) {
            RunningRateDayInfoData data = new RunningRateDayInfoData();
            data.setCreateTime(dateFormat.parse((String) dataMap.get("createtime")));
            data.setD_ycjone_sc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjone_sc")));
            data.setD_ycjone_zsc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjone_zsc")));
            data.setD_ycjone_yzl(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjone_yzl")));
            data.setD_ycjtwo_sc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjtwo_sc")));
            data.setD_ycjtwo_zsc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjtwo_zsc")));
            data.setD_ycjtwo_yzl(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjtwo_yzl")));
            data.setD_ycjthree_sc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjthree_sc")));
            data.setD_ycjthree_zsc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjthree_zsc")));
            data.setD_ycjthree_yzl(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjthree_yzl")));
            data.setD_ycjfour_sc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjfour_sc")));
            data.setD_ycjfour_zsc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjfour_zsc")));
            data.setD_ycjfour_yzl(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ycjfour_yzl")));
            data.setD_ecj_sc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ecj_sc")));
            data.setD_ecj_zsc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ecj_zsc")));
            data.setD_ecj_yzl(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ecj_yzl")));
            data.setD_ylc_sc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ylc_sc")));
            data.setD_ylc_zsc(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ylc_zsc")));
            data.setD_ylc_yzl(java.math.BigDecimal.valueOf((Double) dataMap.get("d_ylc_yzl")));
            resultList.add(data);
        }
        return resultList;
    }
}